// pages/getTicket/getTicket.js
const HOST = require('../../config.js').host
let app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    ticketList:[],
  },
  getUserTicketInfo(userId){
    let that = this
    wx.request({
      url: `${HOST}v1/coupons/findAllAndMark/${userId}`,
      method:'GET',
      success:res=>{
        let {
          data,
          sysCode
        } = res.data
        console.log(data)
        if (sysCode === 1) {
          that.setData({
            ticketList: data
          })
        }
      }
    })
  }
  ,
  getTicket(e){
    let that = this
    let couponId = '';
    let hasCoupon = 0;
    let index = 0;
    let productId = '';
    couponId = e.currentTarget.dataset.couponid
    hasCoupon = e.currentTarget.dataset.hascoupon
    index = e.currentTarget.dataset.index
    productId = e.currentTarget.dataset.productid
    if (hasCoupon === 0){
        wx.request({
          url: `${HOST}v1/usercoupons/saveOneUserCoupon`,
          method:'POST',
          data:{
            userId:app.userId,
            couponId:couponId
          },
          success:res=>{
            var param = {};
            var string = `ticketList[${index}].hasCoupon`
            param[string] = 1;    
            that.setData(param);
            console.log(that.data)
          }
        })
    }else{
      wx.navigateTo({
        url: `../detail/detail?id=${productId}`,
      })
    }
  }
  ,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.getUserTicketInfo(options.userId)
  },
})